package com.china08.yxyapi.api.manage.edu.platform.resource.yxylectureprepack;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.res.YxyResResource;
import com.china08.yxyapi.entity.es.ElasticYxyYxyQuesResource;
import com.china08.yxyapi.entity.mg.lecture.YxyLectureRes;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.repository.db.YxyResResourceRepository;
import com.china08.yxyapi.repository.mg.lecture.YxyLectureResRepository;
import com.china08.yxyapi.service.manage.tec.teaching.lecture.ElasticQuesService;
import com.china08.yxyapi.service.yxyresource.YxyResourceService;
import com.china08.yxyapi.util.EsResourcePvUpdateUtil;
import com.china08.yxyapi.util.EsResourceUpdateUtil;
import com.china08.yxyapi.util.YxyResUtil;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class YxyLecturePrePackResourceESController
        extends YxyApiBaseController {

    @Autowired
    YxyResUtil yxyresutil;

    @Autowired
    YxyResourceService yxyResourceService;

    @Autowired
    YxyResResourceRepository yxyResResourceRepository;

    @Autowired
    YxyLectureResRepository yxyLectureResRepository;

    @Autowired
    ElasticQuesService elasticQuesService;

    @ApiOperation(value = "备课包资源详情")
    @RequestMapping(value = "/api_yw/manage/edu/platform/resource/lectureprepack/resource/es", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(HttpServletRequest request,
            HttpServletResponse response, @RequestBody ReqModel reqModel)
                    throws JsonParseException, JsonMappingException,
                    IOException {

        String resourceId = reqModel.resourceId;
        Integer resourceType = reqModel.resourceType;
        EsResourcePvUpdateUtil.updateEsResoure(resourceId + "_1");
        // 资源
        if (resourceType == 1) {
            YxyResResource resource = yxyResResourceRepository
                    .findById(resourceId);
            Assert.notNull(resource, "资源找不到了");
            resource.setPv(resource.getPv() + 1);
            yxyResResourceRepository.save(resource);
            // 通知ES更改资源的变更
            EsResourceUpdateUtil.updateEsResoure(resource.getId());

            RespModel respModel = new RespModel();
            String preview = yxyresutil.getPreviewUrl(resourceId);
            List<QuesModel> mulQuesList = new ArrayList<QuesModel>();
            List<QuesModel> gapQuesList = new ArrayList<QuesModel>();
            List<QuesModel> ansQuesList = new ArrayList<QuesModel>();
            List<QuesModel> sinQuesList = new ArrayList<QuesModel>();
            respModel.examine = preview;
            respModel.ansQuesList = ansQuesList;
            respModel.gapQuesList = gapQuesList;
            respModel.mulQuesList = mulQuesList;
            respModel.sinQuesList = sinQuesList;
            return respModel;
        }
        // 试题
        if (resourceType == 0) {
            YxyLectureRes yxyLectureRes = yxyLectureResRepository
                    .findByResourceId(resourceId);
            List<String> quesIds = yxyLectureRes.getQuesIds();

            if (quesIds.size() > 0) {
                List<ElasticYxyYxyQuesResource> quesIdIn = elasticQuesService
                        .findByQuesIdIn(quesIds);
                List<QuesModel> mulQuesList = new ArrayList<QuesModel>();
                List<QuesModel> gapQuesList = new ArrayList<QuesModel>();
                List<QuesModel> ansQuesList = new ArrayList<QuesModel>();
                List<QuesModel> sinQuesList = new ArrayList<QuesModel>();
                QuesModel quesModel = null;
                for (ElasticYxyYxyQuesResource eQues : quesIdIn) {
                    String questionType = eQues.getQuestionType();
                    if (StringUtils.equals(questionType, "8")) {
                        quesModel = new QuesModel();
                        quesModel.quesId = eQues.getId();
                        quesModel.quesBody = eQues.getQuesBody();
                        quesModel.difficultyLevel = eQues.getDifficultyLevel();
                        quesModel.knowledgeTexts = eQues.getKnowledgeTexts();
                        quesModel.questionType = eQues.getQuestionType();
                        // 获取多选题
                        mulQuesList.add(quesModel);
                    } else if (StringUtils.equals(questionType, "1")) {
                        quesModel = new QuesModel();
                        quesModel.quesId = eQues.getId();
                        quesModel.quesBody = eQues.getQuesBody();
                        quesModel.difficultyLevel = eQues.getDifficultyLevel();
                        quesModel.knowledgeTexts = eQues.getKnowledgeTexts();
                        quesModel.questionType = eQues.getQuestionType();
                        // 获取单选题
                        sinQuesList.add(quesModel);
                    } else if (StringUtils.equals(questionType, "2")) {
                        quesModel = new QuesModel();
                        quesModel.quesId = eQues.getId();
                        quesModel.quesBody = eQues.getQuesBody();
                        quesModel.difficultyLevel = eQues.getDifficultyLevel();
                        quesModel.knowledgeTexts = eQues.getKnowledgeTexts();
                        quesModel.questionType = eQues.getQuestionType();
                        // 获取填空题
                        gapQuesList.add(quesModel);
                    } else {
                        quesModel = new QuesModel();
                        quesModel.quesId = eQues.getId();
                        quesModel.quesBody = eQues.getQuesBody();
                        quesModel.difficultyLevel = eQues.getDifficultyLevel();
                        quesModel.knowledgeTexts = eQues.getKnowledgeTexts();
                        quesModel.questionType = eQues.getQuestionType();
                        // 获取解答题
                        ansQuesList.add(quesModel);
                    }
                }
                RespModel respModel = new RespModel();
                respModel.examine = "";
                respModel.ansQuesList = ansQuesList;
                respModel.gapQuesList = gapQuesList;
                respModel.mulQuesList = mulQuesList;
                respModel.sinQuesList = sinQuesList;
                return respModel;
            }
        }

        return null;
    }

    public static class QuesModel {

        // 题id
        public String quesId;

        // 题干
        public Object quesBody;

        // 题型难度
        public String difficultyLevel;

        // 试题涉及的知识点
        public List<String> knowledgeTexts;

        public String questionType;

    }

    public static class ReqModel extends YxyApiPageReqModel {
        public String resourceId;
        public Integer resourceType;

        @Override
        public void autoCheck() {
            Assert.isTrue(StringUtils.isNotBlank(resourceId), "备课包为空");
        }
    }

    public static class RespModel {
        public String examine;

        // 多选题
        public List<QuesModel> mulQuesList;
        // 单选题
        public List<QuesModel> sinQuesList;
        // 填空题
        public List<QuesModel> gapQuesList;

        // 解答题
        public List<QuesModel> ansQuesList;
    }

}
